﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using PSE.AuditLogging.StorageService.Management.ServiceContracts;
using PSE.AuditLogging.StorageService.ServiceImplementation;
using PSE.AuditLogging.Common.Service;
using PSE.Framework.ErrorLogging;


namespace PSE.AuditLogging.StorageService
{
    public partial class Service1 : ServiceBase
    {
        private ServiceHoster _serviceManagerHost;

        public Service1()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            try
            {
                ServiceExecutor executor = ServiceExecutor.GetInstance();

                executor.Start();

                if (!executor.IsRunning)
                    this.Stop();
                else
                {
                    _serviceManagerHost = new ServiceHoster(Constants.SERVICE_MANAGER_NETPIPE_ADDRESS, typeof(IServiceManager), typeof(ServiceManager));
                    _serviceManagerHost.Open();
                }
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
                throw;
            }
        }
        protected override void OnStop()
        {
            try
            {
                ServiceExecutor.GetInstance().Stop();
            }
            catch (Exception ex)
            {
                ErrorLogger.WriteLog(LogType.Error, ex);
            }
        }
    }
}
